package productModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

import spUtil.DB;

public abstract class ProductItem {

	String productName;
	int productId;
	ArrayList<ProductEAV> itemDetails;
	
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public int getProductId() {
		return productId;
	}
	public void setProductId(int productId) {
		this.productId = productId;
	}
	public ArrayList<ProductEAV> getItemDetails() {
		return itemDetails;
	}
	public void setItemDetails(ArrayList<ProductEAV> itemDetails) {
		this.itemDetails = itemDetails;
	}
	public static String getProductNamebyProductId(int productId){
		
		String productName = null;
		ResultSet rs = null;
		String query = "SELECT productName FROM products WHERE productId = " + productId;
		try {
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while (rs.next()) {
				productName = rs.getString("productName");		
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return productName;
	}
	
	public static String getProductCategorybyProductId(int productId){
		
		String productCategory = null;
		ResultSet rs = null;
		String query = "SELECT * from productseav where productId = " + productId;
		try {
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while (rs.next()) {
				if(rs.getString("prodAttribute").equalsIgnoreCase("category")){
					productCategory = rs.getString("prodValue");
					break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return productCategory;
	}
	
	public static String getProductCategorybyProductEAV(ArrayList<ProductEAV> product){
		
		String productCategory = null;
		for(int i=0; i < product.size(); i++){

			if(product.get(i).getProdAttribute().equalsIgnoreCase("category")){
				productCategory = product.get(i).getProdValue();
				break;
			}
		}
		return productCategory;
	}
	
	public static String getProductSubCategorybyProductId(int productId){
		
		String productSubCategory = null;
		ResultSet rs = null;
		String query = "SELECT * from productseav where productId = " + productId;
		try {
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while (rs.next()) {
				if(rs.getString("prodAttribute").equalsIgnoreCase("subCategory")){
					productSubCategory = rs.getString("prodValue");
					break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return productSubCategory;
	}
	
	public static int getProductPricebyProductId(int productId){
		
		int productPrice = 0;
		ResultSet rs = null;
		String query = "SELECT * from productseav where productId = " + productId;
		try {
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while (rs.next()) {
				if(rs.getString("prodAttribute").equalsIgnoreCase("price")){
					productPrice = rs.getInt("prodValue");
					break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return productPrice;
	}
	
	public static String getProductDescbyProductId(int productId){
		
		String productDesc = null;
		ResultSet rs = null;
		String query = "SELECT * from productseav where productId = " + productId;
		try {
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while (rs.next()) {
				if(rs.getString("prodAttribute").equalsIgnoreCase("desc")){
					productDesc = rs.getString("prodValue");
					break;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return productDesc;
	}
	
	public static ArrayList<String> getProductImagesbyProductId(int productId){
		
		ArrayList<String> productImages = new ArrayList<String>();
		ResultSet rs = null;

		String query = "SELECT * from productseav where productId = " + productId;
		try {
			Connection conn = DB.getConnection();
			PreparedStatement st = conn.prepareStatement(query);
			rs = st.executeQuery();
			while (rs.next()) {
				if(rs.getString("prodAttribute").equalsIgnoreCase("imageId")){
					productImages.add(rs.getString("prodValue"));	
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return productImages;
	}
	
	//functions to be over-ridden:
	
	public void getProductDetailsFromEAV(ArrayList<ProductEAV> prodEAV){
		return;
	}
	public String getCategory(){
		return("Call from wrong object.");
	}
	public String getSubCategory(){
		return("Call from wrong object.");
	}
	public String getAttributes() {
		return("Call from wrong object.");
	}
	public String getPrice(){
		return("Call from wrong object.");
	}
	public String getDesc(){
		return("Call from wrong object.");
	}
	public String getImage1(){
		return("Call from wrong object.");
	}
	public String getImage2(){
		return("Call from wrong object.");
	}
	public String getImage3(){
		return("Call from wrong object.");
	}
	public String getImage4(){
		return("Call from wrong object.");
	}
}
